package gui;

import javax.swing.JPanel;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import ctr.RRctr;
import model.Room;
import model.RoomReservation;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddRoom extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -599638447620688873L;
	private RRctr ctr = new RRctr();
	private JList lstRoom;
	private JList lstRes;
	private Room room;
	private RoomReservation res;

	/**
	 * Create the panel.
	 */
	public AddRoom() {
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 225, 252);
		add(scrollPane);
		
		lstRoom = new JList();
		scrollPane.setViewportView(lstRoom);
		
		
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(225, 0, 225, 252);
		add(scrollPane_1);
		
		lstRes = new JList();
		scrollPane_1.setViewportView(lstRes);
		
		JButton btnShowRoom = new JButton("Show Room");
		btnShowRoom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateRoomList();
			}
		});
		btnShowRoom.setBounds(0, 250, 107, 25);
		add(btnShowRoom);
		
		JButton btnChoseGuest = new JButton("Chose Room");
		btnChoseGuest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				choseRoom();
			}
		});
		btnChoseGuest.setBounds(105, 250, 107, 25);
		add(btnChoseGuest);
		
		JButton btnShowReservation = new JButton("Show Reservation");
		btnShowReservation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateRoomReservationList();
			}
		});
		btnShowReservation.setBounds(315, 250, 135, 25);
		add(btnShowReservation);
		
		JButton btnNewButton = new JButton("Chose Reservation");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				choseReservation();
			}
		});
		btnNewButton.setBounds(315, 275, 135, 25);
		add(btnNewButton);
		
		JButton btnAddroom = new JButton("AddRoom");
		btnAddroom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				makeBuffer();
			}
		});
		btnAddroom.setBounds(178, 275, 97, 25);
		add(btnAddroom);
		
		

	}
	
	private void updateRoomList()
	{
		ArrayList<Room> list = new RRctr().findAllRooms();
		DefaultListModel<Room> dlist = new DefaultListModel<>();
		for(Room room: list)
			dlist.addElement(room);
		lstRoom.setModel(dlist);
	}
	
	private void updateRoomReservationList()
	{
		ArrayList<RoomReservation> list = new RRctr().findAllReservation();
		DefaultListModel<RoomReservation> dlist = new DefaultListModel<>();
		for(RoomReservation room: list)
			dlist.addElement(room);
		lstRes.setModel(dlist);
	}
	
public void choseRoom() {
		
		this.room = (Room)this.lstRoom.getSelectedValue();
	}

public void choseReservation() {
	
	this.res = (RoomReservation)this.lstRes.getSelectedValue();
}

public void makeBuffer()
{
	int resId = res.getId();
	int roomId = room.getId();
	ctr.insertBuffer(resId, roomId);
	}



}
